// pages/published/published.js
import Toast from '../../vant-weapp/toast/toast';
var app = getApp();
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    activeNames: ['1'],
    activity_type: 0,     // 活动类型
    not_finished_published_events: {},       //所有未开始活动
    not_finished_published_events_exist: false,
    finished_published_events: {},         //所有已结束活动
    finished_published_events_exist: false,
    user: {}
  },

  onChangeTabs(event) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    this.setData({
      activeNames: ['1'],
      activity_type: event.detail.index,
      not_finished_published_events: [],
      finished_published_events: [],
      not_finished_published_events_exist: false,
      finished_published_events_exist: false
    })
    var activity=String(this.data.activity_type+1);
    //查找未开始的任务
    wx.cloud.callFunction({
      name: 'getPublishedEvent',
      data: {
        userId: this.data.user._openid,
        end: false,
        type: activity
      }
    })
    .then(res => {
      if(res.result == undefined){
        this.setData({
          not_finished_published_events_exist: false,
          not_finished_published_events: res.result
        })
      }
      else{
        this.setData({
          not_finished_published_events_exist: Boolean(res.result.length),
          not_finished_published_events: res.result
        })
      }
    })
    .catch(res => console.log(res))
    //查找已结束的活动
    wx.cloud.callFunction({
      name: 'getPublishedEvent',
      data: {
        userId: this.data.user._openid,
        end: true,
        type: activity
      }
    })
    .then(res => {
      wx.hideToast()
      if(res.result == undefined){
        this.setData({
          finished_published_events_exist: false,
          finished_published_events: res.result
        })
      }
      else{
        this.setData({
          finished_published_events_exist: Boolean(res.result.length),
          finished_published_events: res.result
        })
      }
    })
    .catch(res => console.log(res))
    this.setData({
      activity_type: event.detail.index
    })
  },

  onChangeCollapse(event) {
    this.setData({
      activeNames: event.detail,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    });
    //查找用户信息
    db.collection("user").where({
      _openid: app.globalData.open_id,
    }).get().then(res => {
      this.setData({
        user: res.data[0]
      })
      //查找未开始的活动
      wx.cloud.callFunction({
        name: 'getPublishedEvent',
        data: {
          userId: this.data.user._openid,
          end: false,
          type: "1"
        }
      })
      .then(res => {
        if(res.result == undefined){
          this.setData({
            not_finished_published_events_exist: false,
            not_finished_published_events: res.result
          })
        }
        else{
          this.setData({
            not_finished_published_events_exist: Boolean(res.result.length),
            not_finished_published_events: res.result
          })
        }
      })
      .catch(res => console.log(res))
      //查找已结束的活动
      wx.cloud.callFunction({
        name: 'getPublishedEvent',
        data: {
          userId: this.data.user._openid,
          end: true,
          type: "1"
        }
      })
      .then(res => {
        wx.hideToast()
        if(res.result == undefined){
          this.setData({
            finished_published_events_exist: false,
            finished_published_events: res.result
          })
        }
        else{
          this.setData({
            finished_published_events_exist: Boolean(res.result.length),
            finished_published_events: res.result
          })
        }
      })
      .catch(res => console.log(res))
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var activity=String(this.data.activity_type+1);
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    });
    //查找未开始的活动
    wx.cloud.callFunction({
      name: 'getPublishedEvent',
      data: {
        userId: this.data.user._openid,
        end: false,
        type: activity
      }
    })
    .then(res => {
      if(res.result == undefined){
        this.setData({
          not_finished_published_events_exist: false,
          not_finished_published_events: res.result
        })
      }
      else{
        this.setData({
          not_finished_published_events_exist: Boolean(res.result.length),
          not_finished_published_events: res.result
        })
      }
    })
    .catch(res => console.log(res))
    //查找已结束的活动
    wx.cloud.callFunction({
      name: 'getPublishedEvent',
      data: {
        userId: this.data.user._openid,
        end: true,
        type: activity
      }
    })
    .then(res => {
      wx.hideToast()
      if(res.result == undefined){
        this.setData({
          finished_published_events_exist: false,
          finished_published_events: res.result
        })
      }
      else{
        this.setData({
          finished_published_events_exist: Boolean(res.result.length),
          finished_published_events: res.result
        })
      }
    })
    .catch(res => console.log(res))
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})